package yxy.game.pm2.active.command.bottlewish;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actwishshop;
import yxy.cherry.data.bean.charge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_心愿瓶;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actfavoritewarlord.FavouritePackage;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.timeline.Timeline;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BottleWishMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.*;

/****************************************
 * @name CMD_Active_心愿瓶_BottleWishShopInfo
 * @description 心愿瓶活动商城信息
 * @author 梁健彬
 * @since 2022-08-22
 ***************************************/
@MessageType(proto = Command.BottleWishShopInfo, classtype = BottleWishMessage.BottleWishShopInfoRequest.class)
public class CMD_Active_心愿瓶_BottleWishShopInfo extends Command<BottleWishMessage.BottleWishShopInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, BottleWishMessage.BottleWishShopInfoRequest request) throws Exception {
        String playercode = messageSource.getTag();
        BottleWishMessage.BottleWishShopInfoResponse.Builder response = BottleWishMessage.BottleWishShopInfoResponse.newBuilder();
        ActiveModule_心愿瓶 am = ActiveModule_心愿瓶.get(playercode, false);
        try {

            if (am == null) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            if (!am.running()) {
                send(messageSource, response, Result.ActNoOpen);
                return;
            }

            //商品表
            Map<String, actwishshop> map = DataCenter.getAllData(actwishshop.class);

            //返回商品信息
            ArrayList<BottleWishMessage.BottleWishShop> list = getFavouritePackage(am, map);

            response.addAllShop(list);

            //开始结束时间
            Timeline timeline = am.getTimeline();
            long startTime = timeline.getStartTime().getMillis();
            long endTime = timeline.getEndTime().getMillis();

            //返回
            response.setBeginTime(startTime);
            response.setEndTime(endTime);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    /**
     * 获取商品信息
     * @param am
     * @param map
     * @return
     */
    private ArrayList<BottleWishMessage.BottleWishShop> getFavouritePackage(ActiveModule_心愿瓶 am, Map<String, actwishshop> map) {

        ArrayList<BottleWishMessage.BottleWishShop> list = new ArrayList<>();

        for (FavouritePackage fp : am.getDgifts()) {

            BottleWishMessage.BottleWishShop.Builder build = BottleWishMessage.BottleWishShop.newBuilder();

            actwishshop shopdata = map.get(String.valueOf(fp.getId()));

            build.setId(shopdata.getId());//表id
            build.setGoodsid(shopdata.getPurchasever());//商品id
            build.setLimitnum(shopdata.getNumber());//限购数量
            build.setBuynum(fp.getBuyNum());//购买的数量
            Currencies curs = new Currencies(Currency.create(shopdata.getRewardtype(), shopdata.getRewardid(), shopdata.getRewardnum())); //奖励
            Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
            build.addAllCurs(items);//商品物品
            charge charge = DataCenter.getData(fp.getGoodsid(), yxy.cherry.data.bean.charge.class);
            build.setMoney(charge.getMoney());//价格

            //是否可购买：通过判断购买次数是否>=限购次数
            int canget = fp.getBuyNum() >= fp.getNumlimit() ? DrawStatus.cannotget.getStatus() : DrawStatus.canget.getStatus();
            build.setCanget(canget);//是否可购买
            list.add(build.build());
        }

        //从低到高排序
        Collections.sort(list, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                BottleWishMessage.BottleWishShop builder1 = (BottleWishMessage.BottleWishShop) o1;
                BottleWishMessage.BottleWishShop builder2 = (BottleWishMessage.BottleWishShop) o2;
                return builder1.getMoney() - builder2.getMoney();
            }
        });

        return list;
    }


    private void send(MessageSource messageSource, BottleWishMessage.BottleWishShopInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }




}
